package com.qch.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author qch
 * @Date 2021/5/20
 */
@Configuration
@ComponentScan("com.qch")
@EnableRabbit
public class RabitConfig {

    //业务订单队列
    public static String BIZ_ORDER_QUEUE = "biz.order.queue";
    //订单交换机
    public static String BIZ_ORDER_EXCHANGE = "biz.order.exchange";
    //订单路由key
    public static String BIZ_ORDER_ROUTING_KEY = "biz.order.key";
    //死信队列
    public static String DLX_ORDER_QUEUE = "dlx.order.queue";
    //死信交换机
    public static String DLX_ORDER_EXCHANGE = "dlx.order.exchange";
    //死信路由key
    public static String DLX_ORDER_ROUTING_KEY = "dlx.order.key";

    /**
     * 业务队列
     * @return
     */
    @Bean
    public Queue queue(){
        Map<String, Object> props = new HashMap<>();
        // 消息的生存时间 10s
        props.put("x-message-ttl", 30000);
        // 设置该队列所关联的死信交换器（当队列消息TTL到期后依然没有消费，则加 入死信队列）
        props.put("x-dead-letter-exchange", DLX_ORDER_EXCHANGE);
        // 设置该队列所关联的死信交换器的routingKey，如果没有特殊指定，使用原 队列的routingKey
        props.put("x-dead-letter-routing-key", DLX_ORDER_ROUTING_KEY);
        Queue queue = new Queue(BIZ_ORDER_QUEUE, true, false, false, props);
        return queue;
    }

    /**
     * 业务路由器
     * @return
     */
    @Bean
    public Exchange exchange(){
        DirectExchange exchange = new DirectExchange(BIZ_ORDER_EXCHANGE, true, false, null);
        return exchange;  }

    /**
     * 业务队列和路由器绑定
     * @return
     */
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(exchange()).with(BIZ_ORDER_ROUTING_KEY).noargs();
    }

    @Bean
    public Queue queueDlx() {
        Queue queue = new Queue(DLX_ORDER_QUEUE, true, false, false);
        return queue;
    }
    /**
     * * 死信交换器
     * * @return
     * */
    @Bean
    public Exchange exchangeDlx() {
        DirectExchange exchange = new DirectExchange(DLX_ORDER_EXCHANGE, true, false, null);
        return exchange;
    }
    /**
     * * 死信交换器绑定死信队列
     * * @return
     * */
    @Bean
    public Binding bindingDlx() {
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with(DLX_ORDER_ROUTING_KEY).noargs();
    }
    @Bean
    @Autowired
    public RabbitAdmin rabbitAdmin(ConnectionFactory factory){
        return new RabbitAdmin(factory);
    }

    @Bean
    @Autowired
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory){
        return new RabbitTemplate(factory);
    }
    @Bean
    @Autowired
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }
}
